package
{
	import actor.Demon;
	
	import capricornus.Capricornus;
	import capricornus.actor.Avatar;
	import capricornus.actor.attribute.CharacterAttribute;
	import capricornus.core.RefreshManager;
	import capricornus.core.StageManager;
	import capricornus.events.LoaderEvent;
	import capricornus.io.KeyboardManager;
	import capricornus.net.loader.SingleUrlLoader;
	
	import components.gameinfo.GameInfoController;
	import components.toolbar.ToolBarController;
	
	import config.DemonConfig;
	
	import flash.display.Sprite;
	import flash.display.StageQuality;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLLoaderDataFormat;
	
	import res.DefaultResManager;
	
	import scene.Scene;
	
	[SWF(width="1000", height="900", frameRate="50", backgroundColor="#eeeeee")]
	public class Main extends Sprite
	{
		private var _loader:SingleUrlLoader;
		
		public function Main()
		{
			if( stage != null )
			{
				init();
			}
			else
			{
				addEventListener( Event.ADDED_TO_STAGE, onAddToStage, false, 0, true );
			}
		}
		
		private function onAddToStage( e:Event ):void
		{
			removeEventListener( Event.ADDED_TO_STAGE, onAddToStage );
			init();
		}
		
		private function init():void
		{
			stage.quality = StageQuality.LOW;
			_loader = new SingleUrlLoader( URLLoaderDataFormat.TEXT );
			_loader.addEventListener( LoaderEvent.LOAD_COMPLETE, onConfigLoadComplete, false, 0, true );
			_loader.load( "resources\\xml\\demonconfig.xml" );
		}
		
		private function onConfigLoadComplete( e:LoaderEvent ):void
		{
			trace( "load complete" );
			DemonConfig.instance.addData( e.data as String );
			
			Capricornus.initialize( stage );
			RefreshManager.instance.start();
			
			initAvatar();
			initComponents();
			
			initListener();
		}
		
		private function initAvatar():void
		{
			var ac:Avatar = Avatar.instance;
			ac.setLocation( 500, 400 );
			var att:CharacterAttribute = ac.attribute as CharacterAttribute;
			att.setActionImagePath( "EM_10012" );
			att.setArmor( 1 );
			att.setHp( 80 );
			att.setID( 1 );
			att.setSpeed( 300 );
//			ac.stand();
//			ac.activate();
		}
		
		private function initComponents():void
		{
			DefaultResManager.initialize();
			new Scene();
			StageManager.stage.addChildAt( Scene.instance, 0 );
//			Scene.instance.addActor( Avatar.instance );
			
			new GameInfoController();
//			new ToolBarController();
			
			Scene.instance.y = 22;
		}
		
		private function initListener():void
		{
			KeyboardManager.instance.start();
			stage.addEventListener( MouseEvent.CLICK, onClickStage, false, 0, true );
		}
		
		private function onClickStage( e:MouseEvent ):void
		{
//			var demon:Demon = Demon.createByID( Math.random()*7+1 );
//			Scene.instance.addDemon( demon );
		}
	}
}












